package com.dragon.client.controller.customer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.client.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.customer.FollowService;
import com.dragon.repository.domain.dto.FollowDTO;
import com.dragon.repository.domain.vo.FollowVO;
import com.dragon.repository.entity.Follow;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * dragon 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-06-20
 */
@RestController
@Api(tags = "跟进记录")
@RequestMapping("follow")
public class FollowController {

    @Autowired
    FollowService service;

    @GetMapping("followList")
    @ApiOperation("跟进记录列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", value = " 搜索关键字", required = false, type = "string"),
            @ApiImplicitParam(name = "adminId", value = "创建人id", required = false, type = "int"),
            @ApiImplicitParam(name = "customerId", value = "客户id", required = false, type = "int"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int")
    })
    public ResponseData<List<FollowDTO>> followList(
            @RequestParam(required = false) String search,
            @RequestParam(required = false) Integer adminId,
            @RequestParam(required = false) Integer customerId,
            @RequestParam Integer page,
            @RequestParam Integer limit){
        Page<Follow> followPage = service.followList(search, adminId, customerId, page, limit);
        return ResponseData.success(FollowDTO.convertList(followPage.getRecords()),followPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation("添加跟进记录")
    @Log
    public ResponseData addFollow(@RequestBody @Validated(Insert.class) FollowVO vo){
        service.addFollow(FollowVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation("更改跟进记录")
    @Log
    public ResponseData updateFollow(@RequestBody @Validated(Update.class) FollowVO vo){
        service.updateFollow(FollowVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除跟进记录")
    @Log
    public ResponseData deleteFollow(@RequestParam
                                     @ApiParam(name = "id", value = "跟进记录id", required = true, type = "int") Integer id){
        service.deleteFollow(id);
        return ResponseData.success();
    }

    @PostMapping("listById")
    @ApiOperation("根据自身id查看详情")
    public ResponseData listById(@RequestParam
                                 @ApiParam(name = "id", value = "跟进记录id", required = true, type = "int")Integer id){
        return ResponseData.success(service.listById(id));
    }

}
